package demo.database.MariaDB.utils;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.titan.core.utils.db.DataSourceUtils;
import com.titan.core.utils.dbinfo.dialect.DBDialect;
import com.titan.core.utils.dbinfo.dialect.IDBDialect;

import demo.database.H2.D02_性能测试.T02_查询性能测试;
import demo.database.MariaDB.MariaDBConstant;

public class MariaDBUtils {
    private volatile static DataSource dataSource = null;

    public static DataSource getDataSource() {
        if(dataSource==null) {
            synchronized (T02_查询性能测试.class) {
                if(dataSource==null) {
                    dataSource = DataSourceUtils.registerMariaDBDataSource(null
                            , MariaDBConstant.IP
                            , MariaDBConstant.PORT
                            , MariaDBConstant.SERVICE_NAME
                            , MariaDBConstant.USERNAME
                            , MariaDBConstant.PASSWORD
                            , 1, 5);
                }
            }
        }
        return dataSource;        
    }
    public static Connection getConnection() throws SQLException {
        DataSource dataSource = getDataSource();
        return dataSource.getConnection();
    }
    public static Connection getConnection(String url, String userid, String password) throws SQLException {
        IDBDialect dialect = DBDialect.MARIADB.get();
        return dialect.getConnection(url, userid, password);
    }
    
    
    public static void shutdown() {
        DataSourceUtils.close(dataSource);
        dataSource = null;
    }    
}
